package com.atguigu.classroom.vod.service.impl;

import com.atguigu.classroom.model.vod.Chapter;
import com.atguigu.classroom.model.vod.Video;
import com.atguigu.classroom.vo.vod.ChapterVo;
import com.atguigu.classroom.vo.vod.VideoVo;
import com.atguigu.classroom.vod.mapper.ChapterMapper;
import com.atguigu.classroom.vod.mapper.VideoMapper;
import com.atguigu.classroom.vod.service.ChapterService;
import com.atguigu.classroom.vod.utils.QiniuUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {
    @Autowired
    private ChapterMapper chapterMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Override
    public List<ChapterVo> getTreeList(Long courseId) {
        List<ChapterVo> chapterVoList=new ArrayList<>();
        //根据courseId得到课程里面所有章节
        LambdaQueryWrapper<Chapter> chapterQueryWrapper=new LambdaQueryWrapper<>();
        chapterQueryWrapper.eq(Chapter::getCourseId,courseId);
        List<Chapter> chapterList = chapterMapper.selectList(chapterQueryWrapper);
        if(chapterList.size()==0){
            return chapterVoList;
        }
        //根据courseId得到课程里面所有小结
        LambdaQueryWrapper<Video> videoQueryWrapper=new LambdaQueryWrapper<>();
        videoQueryWrapper.eq(Video::getCourseId,courseId);
        List<Video> videoList = videoMapper.selectList(videoQueryWrapper);
        if(videoList.size()==0){
            return chapterList.stream().map(chapter -> {
                        ChapterVo chapterVo=new ChapterVo();
                        BeanUtils.copyProperties(chapter,chapterVo);
                        return chapterVo;
                    }).collect(Collectors.toList());
        }
        //用map集合记录不同章节下的videoVo的list集合，方便后面赋值；
        Map<Long,List<VideoVo>> videoVoMap=new HashMap<>();
        //key是chapterId，value是VideoVo对象
        videoList.stream().map(video -> {
            VideoVo videoVo=new VideoVo();
            BeanUtils.copyProperties(video,videoVo);
            if(videoVoMap.containsKey(video.getChapterId())){
                videoVoMap.get(video.getChapterId()).add(videoVo);
            }else {
                List<VideoVo> videoVoList=new ArrayList<>();
                videoVoList.add(videoVo);
                videoVoMap.put(video.getChapterId(),videoVoList);
            }
            return null;
        }).collect(Collectors.toList());
        chapterVoList=chapterList.stream().map(chapter -> {
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(chapter,chapterVo);
            chapterVo.setChildren(videoVoMap.get(chapterVo.getId()));
            return chapterVo;
        }).collect(Collectors.toList());
        return chapterVoList;
    }

    @Override
    @Transactional
    public void removeChapterAndVideoById(Long id) {
        chapterMapper.deleteById(id);
        LambdaUpdateWrapper<Video> videoUpdateWrapper=new LambdaUpdateWrapper<>();
        videoUpdateWrapper.eq(Video::getChapterId,id);
        List<Video> videos = videoMapper.selectList(videoUpdateWrapper);//先查询，用于删除对象存储里的文件
        videoMapper.delete(videoUpdateWrapper);
        List<String> fileKeys=new ArrayList<>(videos.size());
        videos.stream().map(video -> {
            if(video.getVideoSourceId()!=null){
                fileKeys.add(video.getVideoSourceId());
            }
            return null;
        }).collect(Collectors.toList());
        QiniuUtils.deleteFileList(fileKeys);
    }

}
